﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace XFFSM
{
    public class FSMStateNodeFactory
    {
        public static FSMStateNodeData CreateStateNode(RuntimeFSMController controller, string name, Rect rect, bool defaultState)
        {

            // 判断名称是否重复
            if (controller.states.Where(x => x.name.Equals(name)).FirstOrDefault() != null)
            {

                Debug.LogError("创建状态节点失败,名称重复!");
                return null;
            }

            FSMStateNodeData node = new FSMStateNodeData();
            node.name = name;
            node.rect = rect;

            if (defaultState)
            {
                foreach (var item in controller.states)
                {
                    item.defaultState = false;
                }
            }

            node.defaultState = defaultState;
            controller.states.Add(node);
            EditorUtility.SetDirty(controller);
            AssetDatabase.SaveAssets();
            return node;
        }

        public static FSMStateNodeData CreateStateNode(RuntimeFSMController controller, Rect rect, bool defaultState) {

            return CreateStateNode(controller,GetStateNodeName(controller),rect,defaultState);
        }

        private static string GetStateNodeName(RuntimeFSMController controller) {

            string name = null;
            int i = 1;
            do
            {
                name = string.Format("New State{0}", i);
                i++;
            } while (controller.states.Where(x=>x.name.Equals(name)).FirstOrDefault() != null);
            


            return name;
        }

        public static void DeleteState(RuntimeFSMController controller,FSMStateNodeData state) {

            // 判断 删除的是不是 entry  或者 any 
            if (state.name.Equals(FSMConst.anyState) || state.name.Equals(FSMConst.entryState)) {
                Debug.LogWarningFormat("any 或者 entry 状态不能删除!");
                return;
            }

            // 删除相关的过渡 
            for (int i = 0; i < controller.transitions.Count; i++)
            {
                FSMTransitionData transitionData = controller.transitions[i];
                if (transitionData.fromStateName.Equals(state.name) || transitionData.toStateName.Equals(state.name)) {
                    controller.transitions.RemoveAt(i);
                    i--;
                }
            }

            controller.states.Remove(state);
            EditorUtility.SetDirty(controller);
            // 判断是不是默认状态 
            if (state.defaultState) {
                foreach (var item in controller.states)
                {
                    if (item.Equals(FSMConst.anyState) || item.Equals(FSMConst.entryState)) {
                        continue;
                    }
                    item.defaultState = true;
                    break;
                }
            }
        }

        public static void Rename(RuntimeFSMController controller,FSMStateNodeData node,string newName) {

            if ( node.name.Equals(FSMConst.entryState) || node.name.Equals(FSMConst.anyState) ) {
                return;
            }

            if ( controller.states.Where(x=>x.name.Equals(newName)).FirstOrDefault() !=null ) {
                return;
            }

            // 找到 相关的过渡 进行修改 
            foreach (var item in controller.transitions)
            {
                if (item.fromStateName.Equals(node.name)) {
                    item.fromStateName = newName;
                }

                if (item.toStateName.Equals(node.name)) {
                    item.toStateName = newName;
                }
            }

            node.name = newName;

            EditorUtility.SetDirty(controller);
            AssetDatabase.SaveAssets();

        }

    }

}

